package com.ruoyi.psi.purchase.utils;

import com.ruoyi.psi.purchase.apply.domain.PurchaseApplyList;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import com.ruoyi.utils.UtilsAdd;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 *
 * @author Drj 2021/6/28
 */
public class PurchaseUtils {

    /**
     * 生成新的采购订单记录
     * @param purchaseOrderIdLatest 最近的一条记录
     * @return
     */
    public static String getPurchaseOrderId(String purchaseOrderIdLatest){
        String temp = PurchaseUtils.addZeroForNumLeft(purchaseOrderIdLatest,8);//不足8位，补齐8位
        Calendar nowtime = new GregorianCalendar();
        String time = (String.format("%04d", nowtime.get(Calendar.YEAR)) +
                String.format("%02d", nowtime.get(Calendar.MONTH) + 1) +
                String.format("%02d", nowtime.get(Calendar.DATE))).substring(2);
        String purchaseOrderIdNew = "";
        if (temp.substring(0,6).equals(time)){
            //同一天之后的记录
            purchaseOrderIdNew= UtilsAdd.addOne(temp,1);
            System.out.println("同一天之后的记录:"+purchaseOrderIdNew);
        }
        else {
            //当天第一条记录
            purchaseOrderIdNew = UtilsAdd.addOne(addZeroForNumRight(time,8),1);
            System.out.println("当天第一条记录:"+purchaseOrderIdNew);
        }

        return purchaseOrderIdNew;
    }

    public static void main(String[] args) {
        PurchaseUtils.getPurchaseOrderId("21052501");
    }

    /**
     * 左补零
     * @param str
     * @param strLength 长度
     * @return
     */
    public static String addZeroForNumLeft(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                // sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }

    /**
     * 右补零
     * @param str
     * @param strLength 长度
     * @return
     */
    public static String addZeroForNumRight(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                //sb.append("0").append(str);// 左补0
                 sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }
}
